#include "book.h"
#include "ui_book.h"
#include <QDebug>
#include "login.h"
#include<QMessageBox>
#include<QTabWidget>
book::book(QSqlDatabase &d,QString InId) :
//    QWidget(parent),
    meId(InId),
    db(d),
    ui(new Ui::book),
    userPage(nullptr),
    borPage(nullptr),
    userHisPage(nullptr)
{
    ui->setupUi(this);
//    this->setWindowFlags(Qt::FramelessWindowHint);
//    ui->tabWidget->setTabShape(QTabBar::Rounded);
    initPage();
}

book::~book()
{
    delete ui;
}

void book::closeEvent(QCloseEvent *event)
{
    qDebug()<<"调用";//
    //显示登录页面
//    emit destroyed();
}

void book::on_closeBut_clicked()
{
    QMessageBox messageBox(QMessageBox::NoIcon,
                                   "退出", "你确定要退出吗?",
                                   QMessageBox::Yes | QMessageBox::No);
    int result=messageBox.exec();

    switch (result)
    {
    case QMessageBox::Yes:
        qDebug()<<"Yes";
        close();
        break;
    case QMessageBox::No:
        qDebug()<<"NO";
        break;
    default:
        break;
    }
}

void book::on_backBut_clicked()
{
    emit destroyed();
    close();
}


void book::initPage(){

    userPage=new UserPage(db,meId);
    borPage=new borhispage(db,meId);
    userHisPage=new UserHis(db,meId);

    ui->stackedWidget->addWidget(userPage);
    ui->stackedWidget->addWidget(borPage);
    ui->stackedWidget->addWidget(userHisPage);

    connect(ui->userBut,&QToolButton::clicked,this,&book::dealMeun);
    connect(ui->bookBut,&QToolButton::clicked,this,&book::dealMeun);
    connect(ui->hisBut,&QToolButton::clicked,this,&book::dealMeun);
}

void book::dealMeun(){
    qDebug()<<sender()->objectName();
    auto str = sender()->objectName();
    do{
        if(str=="userBut"){
            ui->stackedWidget->setCurrentIndex(0);
        }
        else if (str=="bookBut") {
            ui->stackedWidget->setCurrentIndex(1);
        }
        else if(str=="hisBut"){
            ui->stackedWidget->setCurrentIndex(2);
        }

    }while (false);
}

